package com.cskaoyan.controller;

import com.cskaoyan.bo.wxcart.WxCartBo;
import com.cskaoyan.bo.wxcart.WxCartDeleteBo;
import com.cskaoyan.bo.wxcart.WxCartCheckedBo;
import com.cskaoyan.service.wxcart.WxCartService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.wxcart.WxCartCheckoutVo;
import com.cskaoyan.vo.wxcart.WxCartFastAddVo;
import com.cskaoyan.vo.wxcart.WxCartIndexVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: mall
 * @description:
 * @author: Mr.Mengmeng
 * @create: 2021-04-13 00:02
 **/
// wx小程序 购物车模块
@RestController
@RequestMapping("wx/cart")
public class WxCartController {

    @Autowired
    WxCartService wxCartService;

    // 获取购物车商品件数 wx/cart/goodscount
    @GetMapping("goodscount")
    public BaseRespVo cartGoodsCount() {

        int count = wxCartService.cartGoodsCount();
        return BaseRespVo.ok(count);
    }

    // 获取购物车的数据 wx/cart/index
    @GetMapping("index")
    public BaseRespVo cartIndex() {
        WxCartIndexVo wxCartIndexVo = wxCartService.cartIndex();
        return BaseRespVo.ok(wxCartIndexVo);
    }

    // 添加商品到购物车 wx/cart/add（加入购物车）
    @PostMapping("add")
    public BaseRespVo cartAdd(@RequestBody WxCartBo bo) {
        WxCartFastAddVo vo = wxCartService.cartAdd(bo);
        BaseRespVo baseRespVo = new BaseRespVo();
        if (vo.getTag() == 0) {
            baseRespVo.setData(wxCartService.cartGoodsCount());
            baseRespVo.setErrno(0);
            baseRespVo.setErrmsg("成功");
        } else {
            baseRespVo.setErrno(711);
            baseRespVo.setErrmsg("库存不足");
        }
        return baseRespVo;
    }

    // 立即购买商品 wx/cart/fastadd（点击立即购买）
    @PostMapping("fastadd")
    public BaseRespVo cartFastAdd(@RequestBody WxCartBo bo) {
        WxCartFastAddVo vo = wxCartService.cartAdd(bo);
        BaseRespVo baseRespVo = new BaseRespVo();
        if (vo.getTag()== 0) {
            baseRespVo.setData(vo.getCartId());
            baseRespVo.setErrno(0);
            baseRespVo.setErrmsg("成功");
        } else {
            baseRespVo.setErrno(711);
            baseRespVo.setErrmsg("库存不足");
        }
        return baseRespVo;
    }

    // 更新购物车的商品 wx/cart/update（更新购物车）
    @PostMapping("update")
    public BaseRespVo cartUpdate(@RequestBody WxCartBo bo) {
        int i = wxCartService.cartUpdate(bo);
        BaseRespVo baseRespVo = new BaseRespVo();
        if (i == 0) {
            baseRespVo.setErrno(0);
            baseRespVo.setErrmsg("成功");
        } else {
            baseRespVo.setErrno(710);
            baseRespVo.setErrmsg("库存不足");
        }
        return baseRespVo;
    }

    // 选择或取消选择商品 wx/cart/checked
    @PostMapping("checked")
    public BaseRespVo cartChecked(@RequestBody WxCartCheckedBo bo) {
        // 逻辑是：选择商品不做返回值，返回信息为调用查看购物车，返回购物车的信息。
        wxCartService.cartChecked(bo);
        return BaseRespVo.ok(wxCartService.cartIndex());
    }

    // 下单前信息确认 wx/cart/checkout
//    @JsonInclude(JsonInclude.Include.NON_NULL)
    @GetMapping("checkout")
    public BaseRespVo cartCheckout(Integer cartId,Integer addressId,Integer couponId,Integer grouponRulesId) {
        WxCartCheckoutVo wxCartCheckoutVo = wxCartService.cartCheckout(cartId, addressId, couponId, grouponRulesId);
        return BaseRespVo.ok(wxCartCheckoutVo);
    }

    // 删除购物车的商品 wx/cart/delete
    @PostMapping("delete")
    public BaseRespVo cartDelete(@RequestBody WxCartDeleteBo wxCartDeleteBo) {
        // 逻辑是：逻辑删除不做返回值，返回信息为调用查看购物车，返回购物车的信息。
        wxCartService.cartDelete(wxCartDeleteBo);
        return BaseRespVo.ok(wxCartService.cartIndex());
    }

}
